package com.bwie.spike.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bwie.spike.dao.OrderInfoDao;
import com.bwie.spike.entity.OrderInfoEntity;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName Consume
 * @Description TODO
 * @Author 李奉武
 * @Motto 逆水行舟, 不进则退
 * @Date 2022/3/2 20:36
 * @Version 1.0
 */
@Component
public class Consume {

    @Autowired
    OrderInfoDao orderInfoDao;

    @Autowired
    StringRedisTemplate stringRedisTemplate;



    @RabbitListener(queues = "07a")
    public void OrderGenerat(Message message, String order, Channel channel){
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            OrderInfoEntity orderInfoEntity = JSON.parseObject(order, new TypeReference<OrderInfoEntity>() {
            });
            OrderInfoEntity orderInfoEntity1 = orderInfoDao.selectById(orderInfoEntity.getId());
            if (orderInfoEntity1==null){
                stringRedisTemplate.opsForHash().put("order", orderInfoEntity.getId().toString(), JSON.toJSONString(orderInfoEntity));
                int insert = orderInfoDao.insert(orderInfoEntity);
                if (insert!=0){
                    stringRedisTemplate.delete(orderInfoEntity.getUserId()+"user");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
